from django.http import JsonResponse
from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_http_methods
import json

def index(request):
    """API根路径，用于测试连接"""
    return JsonResponse({
        'message': '矿山安全评价系统API',
        'status': 'success',
        'version': '1.0.0'
    })

@csrf_exempt
@require_http_methods(["POST"])
def calculate_safety(request):
    """安全评价计算接口"""
    try:
        data = json.loads(request.body)
        
        # 简化的AHP-MIV-BP计算模拟
        indicators = data.get('indicators', {})
        basic_info = data.get('basic', {})
        
        # 模拟计算过程
        values = [float(v) for v in indicators.values() if v and str(v).replace('.', '').isdigit()]
        if values:
            total_score = sum(values) / len(values)
        else:
            total_score = 0.5
        
        bp_prediction = total_score + (0.1 if total_score > 0.5 else -0.1)
        
        # 确定安全等级
        if total_score >= 0.8:
            level = 'A级 - 安全'
            level_class = 'level-a'
        elif total_score >= 0.6:
            level = 'B级 - 较安全'
            level_class = 'level-b'
        elif total_score >= 0.4:
            level = 'C级 - 较危险'
            level_class = 'level-c'
        else:
            level = 'D级 - 危险'
            level_class = 'level-d'
        
        result = {
            'level': level,
            'levelClass': level_class,
            'score': round(total_score * 100, 1),
            'bpScore': round(bp_prediction, 3),
            'expectedScore': round(total_score, 3),
            'basicInfo': basic_info,
            'calculatedAt': '2025-10-09'
        }
        
        return JsonResponse({
            'status': 'success',
            'data': result
        })
        
    except Exception as e:
        return JsonResponse({
            'status': 'error',
            'message': str(e)
        }, status=400)

def get_dashboard_statistics(request):
    """获取工作台统计数据"""
    return JsonResponse({
        'status': 'success',
        'data': {
            'totalEvaluations': 125,
            'pendingHazards': 23,
            'completedRemediation': 89,
            'dangerousMines': 12,
            'recentActivities': [
                {
                    'id': 1,
                    'type': '',
                    'icon': 'fas fa-calculator',
                    'content': '<strong>张三</strong> 完成了 <strong>东风煤矿</strong> 的安全评价',
                    'time': '2小时前'
                },
                {
                    'id': 2,
                    'type': 'warning',
                    'icon': 'fas fa-exclamation-triangle',
                    'content': '<strong>红星金矿</strong> 被评定为 <strong>较危险</strong> 等级',
                    'time': '4小时前'
                },
                {
                    'id': 3,
                    'type': 'success',
                    'icon': 'fas fa-check-circle',
                    'content': '<strong>李四</strong> 完成了通风设备的整改任务',
                    'time': '1天前'
                }
            ]
        }
    })
